/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lime2.*; 
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * A simple agent performing outg of eight tuples: <code>hello0</code>, <code>hello1</code>, <code>hello2</code>,
 * <code>hello3</code>, <code>hello4</code>, <code>hello5</code>, <code>hello6</code>, <code>hello7</code>.
 * <br>
 * Allows to select destination.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class Outg8 implements Runnable 
{
 //	identification number	
 private int id = new Random().nextInt(1000);		
	
 // private data member
 private Location dest = Location.HERE;
 
 // String value for "here" 
 private final String HereS = "here";
 
 /**
  * Constructor method (default). Destination location is here.
  */
 public Outg8()
 {
  // empty
 }
 
 /**
  * Constructor method.
  * 
  * @param a [here]
  */
 public Outg8(String a)
 {
  if (!a.equals(HereS)) throw new IllegalArgumentException("if you pass only one parameter, it must be " + HereS);
 }
 
 /**
  * Constructor method.
  * 
  * @param a [name/IP of destination location]
  * @param b [port of destination location]
  */
 public Outg8(String a, String b)
 {
  dest = Location.build(a, new Integer(b).intValue());
 }
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("Outg8 " + id + ": started");     
  LimeTupleSpace lts = LimeTupleSpace.get("testing"); 
  ITuple[] tuples = new ITuple[] { 
  	                              new Tuple().add(new Field().setValue("hello0")), 
  	                              new Tuple().add(new Field().setValue("hello1")),
  	                              new Tuple().add(new Field().setValue("hello2")),
                                  new Tuple().add(new Field().setValue("hello3")),
								  new Tuple().add(new Field().setValue("hello4")), 
  	                              new Tuple().add(new Field().setValue("hello5")),
  	                              new Tuple().add(new Field().setValue("hello6")),
                                  new Tuple().add(new Field().setValue("hello7"))
                                 }; 
  LimeTuple[] lt = lts.outg(dest, tuples);                               
  System.out.println("Outg8 " + id + ": performed outg of "  
                    + lt[0].getTuple() + ", " + lt[1].getTuple() + ", " + lt[2].getTuple() + ", " + lt[3].getTuple()
 	   		        + ",\n                          " + emptyString(new Integer(id).toString().length()) 
				    + lt[4].getTuple() + ", " + lt[5].getTuple() + ", " + lt[6].getTuple() + ", " + lt[7].getTuple());
  System.out.println("Outg8 " + id + ": dies");
 }
 
 // private method for formatting textual output: generates an empty string of lenght n (n > 1)
 private String emptyString(int n)
 {
  String ret = " ";	
  for (int i = 1; i < n; i++) ret = " ".concat(ret);   	
  return ret;
 }
 
}
